library(ggplot2)
library(scales)
library(gridExtra)

load("02_06_m5s_blog_mar2015_post.RData")
load("02_06_m5s_blog_mar2015_comment.RData")

# First national assembly
# line1 = as.numeric(as.Date("2009-03-08"))
# First regional election
# line2 = as.numeric(as.Date("2010-03-29"))
# Local elections
# line3 = as.numeric(as.Date("2011-05-16"))
# Local elections
line4 = as.numeric(as.Date("2012-05-07"))
# Sicilian elections
# line5 = as.numeric(as.Date("2012-10-28"))
# National elections
line6 = as.numeric(as.Date("2013-02-25"))
# V-Day
line7 = as.numeric(as.Date("2007-09-08"))
# European election
line8 = as.numeric(as.Date("2014-05-22"))


# Stats
blog_comment_wkly_mean0 <- mean(subset(blog.comment.week, 
                                       as.numeric(as.Date(Var1)) <= line7)[,"Freq"])
blog_comment_wkly_mean1 <- mean(subset(blog.comment.week, 
                                       as.numeric(as.Date(Var1)) > line7 &
                                        as.numeric(as.Date(Var1)) <= line4)[,"Freq"])
blog_comment_wkly_sd <- sd(blog.comment.week$Freq)
blog_comment_wkly_max <- max(blog.comment.week$Freq)


# Plots
blog_ts_post <- ggplot(blog.post.week , aes(x=as.Date(Var1), y=Freq)) + geom_line() + geom_area(fill="gray") +
  labs(x=NULL, y="Posts per week") +
  theme(axis.text.x=element_blank(),
        axis.title.x=element_blank(),
        plot.title=element_blank(),
        axis.ticks.x=element_blank(),
        plot.margin = unit(c(0.5,0.5,0,0.5), "lines"),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line(colour = "gray")) +
  # geom_vline(xintercept=line1, linetype="dotted", size=.2) +
  # geom_vline(xintercept=line2, linetype="dotted", size=.2) +
  # geom_vline(xintercept=line3, linetype="dotted", size=.2) +
  geom_vline(xintercept=line4, linetype="dotted", size=.2) +
  # geom_vline(xintercept=line5, linetype="dotted", size=.2) +
  geom_vline(xintercept=line6, linetype="dotted", size=.2) +
  geom_vline(xintercept=line7, linetype="dotted", size=.2) +
  geom_vline(xintercept=line8, linetype="dotted", size=.2) +
  annotate("text", x=as.Date(line6, origin = "1970-01-01"), y=45, 
           label="National\nelecion", size=4)

blog_ts_comment <- ggplot(blog.comment.week , aes(x=as.Date(Var1), y=Freq)) + geom_line() + geom_area(fill="gray") +
  labs(x=NULL, y="Comments per week ") + scale_y_continuous(labels=comma) +
  theme(# axis.text.x=element_blank(),
        axis.title.x=element_blank(),
        plot.title=element_blank(),
        # axis.ticks.x=element_blank(),
        plot.margin = unit(c(0,0.5,0,0.5), "lines"),
        panel.background = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_blank(),
        axis.line = element_line(colour = "gray")) +
  # geom_vline(xintercept=line1, linetype="dotted", size=.2) +
  # geom_vline(xintercept=line2, linetype="dotted", size=.2) +
  # geom_vline(xintercept=line3, linetype="dotted", size=.2) +
  geom_vline(xintercept=line4, linetype="dotted", size=.2) +
  # geom_vline(xintercept=line5, linetype="dotted", size=.2) +
  geom_vline(xintercept=line6, linetype="dotted", size=.2) +
  geom_vline(xintercept=line7, linetype="dotted", size=.2) +
  geom_vline(xintercept=line8, linetype="dotted", size=.2) +
  annotate("text", x=as.Date(line4, origin = "1970-01-01"), y=30000, 
           label="Local\nelections", size=4) +
  annotate("text", x=as.Date(line7, origin = "1970-01-01"), y=40000, 
           label="First\nV-Day", size=4) + 
annotate("text", x=as.Date(line8, origin = "1970-01-01"), y=60000, 
         label="European\nelection", size=4) +
  geom_smooth(se=FALSE) # +
#   geom_smooth(aes(x=as.Date(date), y=perc*1000), 
#               data=subset(m5s_vote_survey, 
#                           date<=as.Date("2015-02-10")),
#               se=FALSE, colour="red")


gp1<- ggplot_gtable(ggplot_build(blog_ts_post))
gp2<- ggplot_gtable(ggplot_build(blog_ts_comment))
require(grid)
maxWidth = unit.pmax(gp1$widths[2:3], gp2$widths[2:3])
gp1$widths[2:3] <- maxWidth
gp2$widths[2:3] <- maxWidth

grid.arrange(gp1,gp2)
